package com.study.webapp.springThink.lookup;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.HierarchicalBeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @program: study
 *
 * @description:层次性依赖查找
 *
 * @author: heqiang
 *
 * @create: 2020-06-27 21:41
 **/
public class HierarchicalDemo {


    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        //将当前类作为配置类
        applicationContext.register(HierarchicalDemo.class);
        //1获取HierarchicalBeanFactory <- ConfigurableBeanFactory <- ConfigurableListableBeanFactory
        ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();
        System.out.println("当前beanFactory的parentFactory:"+beanFactory.getParentBeanFactory());

        //2,设置beanfactory
        HierarchicalBeanFactory parentBeanFactory = createParentBeanFactory();
        beanFactory.setParentBeanFactory(createParentBeanFactory());
        System.out.println("当前beanFactory的parentFactory222:"+beanFactory.getParentBeanFactory());

        displayContextLocalBean(beanFactory,"user");
        displayContextLocalBean(parentBeanFactory,"user");

        displayContainsBean(beanFactory,"user");
        displayContainsBean(parentBeanFactory,"user");


        //启动
        applicationContext.refresh();

        //关闭应用上下文
        applicationContext.close();
    }

    public static ConfigurableListableBeanFactory createParentBeanFactory() {
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
        //xml路径
        String location = "classpath:/META-INF/bean/dependency-lookup-context.xml";
        reader.loadBeanDefinitions(location);
        return beanFactory;
    }


    public static void displayContextLocalBean(HierarchicalBeanFactory beanFactory,String beanName) {
        System.out.printf("当前Beanfactory[%s] 是否包含bean[name : %s] : %s \n",
                beanFactory,beanName,beanFactory.containsLocalBean(beanName));
    }


    public static void displayContainsBean(HierarchicalBeanFactory beanFactory,String beanName) {
        System.out.printf("当前Beanfactory[%s] 是否包含BEANaaaa[name : %s] : %s \n",
                beanFactory,beanName,containsBean(beanFactory,beanName));
    }

    private static boolean containsBean(HierarchicalBeanFactory beanFactory,String beanName) {
        BeanFactory parentBeanFactory = beanFactory.getParentBeanFactory();
        if (parentBeanFactory instanceof HierarchicalBeanFactory) {
            HierarchicalBeanFactory parenthieratchicalBeanFactory = HierarchicalBeanFactory.class.cast(parentBeanFactory);
            if (containsBean(parenthieratchicalBeanFactory,beanName)) {
                return true;
            }
        }
        return beanFactory.containsLocalBean(beanName);
    }
}
